package com.atguigu.gulimall.cart.controller;

import com.atguigu.gulimall.cart.interceptor.CartInterceptor;
import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.to.UserInfoTo;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @ResponseBody
    @GetMapping("/user/cart")
    public List<CartItem> getUserCartItem(){
        return cartService.getUserCartItem();
    };

    // 删除商品
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    // 加减的单击事件
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.countItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    // 修改商品的选择状态
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check){
        System.out.println("执行到这里了check,skuId=" + skuId + "  check= "+check);
        cartService.checkItem(skuId,check);
        System.out.println("执行结束");
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 浏览器有一个cookie：user-key;标识用户身份,一个月后过期;
     * 如果第一次使用jd的购物车功能,都会给一个临时的用户身份;
     * 浏览器以后保存,每次访问都会带上这个cookie;
     *
     * 登录:session有
     * 没登录:按照cookie里面带来的user-key来做。
     * 第一次:如果没有临时用户,帮忙创建一个临时用户。
     * */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    // 删除购物车
    //@GetMapping("/delete")
    //public String

    // addCart 添加商品进购物车
    /**
     * 为了避免用户一直刷新页面，重复提交数据，通过重定向的方式获取购物车内容
     * RedirectAttributes的addFlashAttribut()方法：将对象存储在Session中且只能使用一次，再次刷新就没有了
     * RedirectAttributes的addAttribut()方法：将对象拼接在url中
     * */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            Model model, RedirectAttributes redirectAttributes)
            throws ExecutionException, InterruptedException {
        CartItem cartItem = cartService.addToCart(skuId,num);
        //model.addAttribute("cartItem",cartItem);
        //redirectAttributes.addFlashAttribute("cartItem",cartItem);
        redirectAttributes.addAttribute("skuId",skuId);
        //return "success";
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId,Model model){
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItem);
        return "success";
    }

    @GetMapping("/cartList.html")
    public String listPage(){
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        System.out.println(userInfoTo);
        return "cartList";
    }

    @GetMapping("/success.html")
    public String listSuccessPage(){
        return "success";
    }

}
